package top.hmtools.jsCss.yui;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringWriter;
import java.io.Writer;

import javax.annotation.PostConstruct;

import org.apache.commons.io.IOUtils;
import org.mozilla.javascript.EvaluatorException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.yahoo.platform.yui.compressor.JavaScriptCompressor;

@Component
public class JsCompressorComponet {
	
	private final Logger logger = LoggerFactory.getLogger(JsCompressorComponet.class);
	
	private YuiLoggerErrorReporter yuiLoggerErrorReporter = null;

	@PostConstruct
	public void init(){
		this.yuiLoggerErrorReporter = new YuiLoggerErrorReporter();
		this.logger.info("初始化OK");
	}
	
	/**
	 * 进行js压缩
	 * @param reader	js内容输入流（压缩前）
	 * @param writer	js内容输出流（压缩后）
	 * @param linebreak	指定列插入换行符，-1 为不插入换行符
	 * @param munge	是否进行混淆
	 * @param verbose	是否打印警告、错误日志
	 * @param preserveAllSemiColons	是否保留所有分号“;”
	 * @param disableOptimizations		是否禁用优化
	 */
	public void compressor(Reader reader,Writer writer, int linebreak, boolean munge, boolean verbose, boolean preserveAllSemiColons, boolean disableOptimizations){
		try {
			JavaScriptCompressor compressor = new JavaScriptCompressor(reader,this.yuiLoggerErrorReporter);
			compressor.compress(writer, linebreak, munge, verbose, preserveAllSemiColons, disableOptimizations);
		} catch (EvaluatorException | IOException e) {
			this.logger.error("压缩js时发生异常：",e);
			throw new RuntimeException(e.getMessage());
		}
	}
	
	/**
	 * 进行js压缩
	 * @param readerStr js文本内容
	 * @param writer	js内容输出流（压缩后）
	 * @param linebreak	指定列插入换行符，-1 为不插入换行符
	 * @param munge	是否进行混淆
	 * @param verbose	是否打印警告、错误日志
	 * @param preserveAllSemiColons	是否保留所有分号“;”
	 * @param disableOptimizations		是否禁用优化
	 * @param charset  字符编码，缺省 utf-8
	 */
	public void compressor(String readerStr,Writer writer, int linebreak, boolean munge, boolean verbose, boolean preserveAllSemiColons, boolean disableOptimizations,String charset){
		if(readerStr == null || readerStr.trim().length()<1){
			return;
		}
		if(charset == null || charset.trim().length()<1){
			charset = "UTF-8";
		}
		InputStream inputStream;
		try {
			inputStream = IOUtils.toInputStream(readerStr,charset);
			Reader reader = new InputStreamReader(inputStream, charset);
			this.compressor(reader, writer, linebreak, munge, verbose, preserveAllSemiColons, disableOptimizations);
		} catch (IOException e) {
			this.logger.error("压缩js时发生异常：{}",e);
			throw new RuntimeException(e.getMessage());
		}catch (IndexOutOfBoundsException e) {
			try {
				inputStream = IOUtils.toInputStream(readerStr,charset);
				Reader reader = new InputStreamReader(inputStream, charset);
				IOUtils.copy(reader, writer);
			} catch (IOException eee) {
				this.logger.error("压缩js时发生异常后，尝试返回原内容异常：",eee);
				throw new RuntimeException(eee.getMessage());
			}
		}
	}
	
	/**
	 * 进行js压缩
	 * @param readerStr js文本内容
	 * @param linebreak	指定列插入换行符，-1 为不插入换行符
	 * @param munge	是否进行混淆
	 * @param verbose	是否打印警告、错误日志
	 * @param preserveAllSemiColons	是否保留所有分号“;”
	 * @param disableOptimizations		是否禁用优化
	 * @param charset		字符编码，缺省 utf-8
	 * @return
	 */
	public String compressor(String readerStr, int linebreak, boolean munge, boolean verbose, boolean preserveAllSemiColons, boolean disableOptimizations,String charset){
		if(readerStr == null || readerStr.trim().length()<1){
			return null;
		}
		if(charset == null || charset.trim().length()<1){
			charset = "UTF-8";
		}
		try {
			InputStream inputStream = IOUtils.toInputStream(readerStr,charset);
			Reader reader = new InputStreamReader(inputStream, charset);
			
			StringWriter writer = new StringWriter();
			this.compressor(reader, writer, linebreak, munge, verbose, preserveAllSemiColons, disableOptimizations);
			return writer.toString();
		} catch (IOException e) {
			this.logger.error("压缩js时发生异常：{}",e);
			throw new RuntimeException(e.getMessage());
		}catch (IndexOutOfBoundsException e){
			return readerStr;
		}
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}
